// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_plan_api_serv

import (
	"context"
	"time"

	"gitcode.com/lvupclub/lvup-api/dao"
	"gitcode.com/lvupclub/lvup-api/dao/roadmap_dao"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_plan_api"
	"github.com/google/uuid"
)

func (apiImpl *RoadmapPlanApiImpl) AddPlan(ctx context.Context, req *roadmap_plan_api.AddPlanRequest) (*roadmap_plan_api.AddPlanResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.AddPlanResponse{
			Code:   roadmap_plan_api.AddPlanResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.AddPlanResponse{
			Code:   roadmap_plan_api.AddPlanResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	//只有路线图创建者有权限
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_plan_api.AddPlanResponse{
			Code:   roadmap_plan_api.AddPlanResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_plan_api.AddPlanResponse{
			Code:   roadmap_plan_api.AddPlanResponse_CODE_NO_TARGET_MEMBER,
			ErrMsg: "目标成员不存在",
		}, nil
	}

	if req.EndTime < req.StartTime {
		req.StartTime, req.EndTime = req.EndTime, req.StartTime
	}

	planId := uuid.NewString()
	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.PlanInfoDao.Insert(sessCtx, &roadmap_dao.PlanInfoDbItem{
			PlanId:       planId,
			RoadmapId:    req.RoadmapId,
			MemberUserId: req.TargetMemberUserId,
			BasicInfo: roadmap_dao.BasicPlanInfoDbItem{
				PlanName: req.BasicInfo.PlanName,
				PlanDesc: req.BasicInfo.PlanDesc,
			},
			StartTime:  req.StartTime,
			EndTime:    req.EndTime,
			CreateTime: nowTime,
			UpdateTime: nowTime,
		})
		if err != nil {
			return nil, err
		}
		count, err := roadmap_dao.PlanInfoDao.Count(sessCtx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		err = roadmap_dao.RoadmapMemberDao.UpdatePlanCount(sessCtx, req.RoadmapId, req.TargetMemberUserId, count)
		if err != nil {
			return nil, err
		}
		return &roadmap_plan_api.AddPlanResponse{
			Code:   roadmap_plan_api.AddPlanResponse_CODE_OK,
			PlanId: planId,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_plan_api.AddPlanResponse), nil
}

func (apiImpl *RoadmapPlanApiImpl) UpdatePlan(ctx context.Context, req *roadmap_plan_api.UpdatePlanRequest) (*roadmap_plan_api.UpdatePlanResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.UpdatePlanResponse{
			Code:   roadmap_plan_api.UpdatePlanResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.UpdatePlanResponse{
			Code:   roadmap_plan_api.UpdatePlanResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	//只有路线图创建者有权限
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_plan_api.UpdatePlanResponse{
			Code:   roadmap_plan_api.UpdatePlanResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	exist, err := roadmap_dao.PlanInfoDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_plan_api.UpdatePlanResponse{
			Code:   roadmap_plan_api.UpdatePlanResponse_CODE_NO_PLAN,
			ErrMsg: "成长计划不存在",
		}, nil
	}

	nowTime := time.Now().UnixMilli()

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.PlanInfoDao.UpdateBasic(sessCtx, req.RoadmapId, req.TargetMemberUserId, req.PlanId,
			&roadmap_dao.BasicPlanInfoDbItem{
				PlanName: req.BasicInfo.PlanName,
				PlanDesc: req.BasicInfo.PlanDesc,
			}, nowTime)
		if err != nil {
			return nil, err
		}
		return &roadmap_plan_api.UpdatePlanResponse{
			Code: roadmap_plan_api.UpdatePlanResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_plan_api.UpdatePlanResponse), nil
}

func (apiImpl *RoadmapPlanApiImpl) ListPlan(ctx context.Context, req *roadmap_plan_api.ListPlanRequest) (*roadmap_plan_api.ListPlanResponse, error) {
	emptyList := []*roadmap_plan_api.PlanInfo{}

	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.ListPlanResponse{
			Code:     roadmap_plan_api.ListPlanResponse_CODE_WRONG_SESSION,
			ErrMsg:   "会话不存在",
			PlanList: emptyList,
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.ListPlanResponse{
			Code:     roadmap_plan_api.ListPlanResponse_CODE_NO_ROADMAP,
			ErrMsg:   "成长路线图不存在",
			PlanList: emptyList,
		}, nil
	}
	if roadmapItem.OwnerUserId == sessItem.UserId {
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.ListPlanResponse{
				Code:     roadmap_plan_api.ListPlanResponse_CODE_NO_TARGET_MEMBER,
				ErrMsg:   "目标成员不存在",
				PlanList: emptyList,
			}, nil
		}
	} else {
		if sessItem.UserId != req.TargetMemberUserId {
			return &roadmap_plan_api.ListPlanResponse{
				Code:     roadmap_plan_api.ListPlanResponse_CODE_NO_PERMISSION,
				ErrMsg:   "没有权限",
				PlanList: emptyList,
			}, nil
		}
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.ListPlanResponse{
				Code:     roadmap_plan_api.ListPlanResponse_CODE_NO_PERMISSION,
				ErrMsg:   "没有权限",
				PlanList: emptyList,
			}, nil
		}
	}

	planItemList, err := roadmap_dao.PlanInfoDao.List(ctx, req.RoadmapId, req.TargetMemberUserId)
	if err != nil {
		return nil, err
	}
	planList := []*roadmap_plan_api.PlanInfo{}
	for _, planItem := range planItemList {
		planList = append(planList, planItem.ToPlanInfo())
	}

	return &roadmap_plan_api.ListPlanResponse{
		Code:     roadmap_plan_api.ListPlanResponse_CODE_OK,
		PlanList: planList,
	}, nil
}

func (apiImpl *RoadmapPlanApiImpl) GetPlan(ctx context.Context, req *roadmap_plan_api.GetPlanRequest) (*roadmap_plan_api.GetPlanResponse, error) {
	emptyInfo := &roadmap_plan_api.PlanInfo{
		BasicInfo: &roadmap_plan_api.BasicPlanInfo{},
	}

	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.GetPlanResponse{
			Code:   roadmap_plan_api.GetPlanResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
			Plan:   emptyInfo,
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.GetPlanResponse{
			Code:   roadmap_plan_api.GetPlanResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
			Plan:   emptyInfo,
		}, nil
	}
	if roadmapItem.OwnerUserId == sessItem.UserId {
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.GetPlanResponse{
				Code:   roadmap_plan_api.GetPlanResponse_CODE_NO_TARGET_MEMBER,
				ErrMsg: "目标成员不存在",
				Plan:   emptyInfo,
			}, nil
		}
	} else {
		if sessItem.UserId != req.TargetMemberUserId {
			return &roadmap_plan_api.GetPlanResponse{
				Code:   roadmap_plan_api.GetPlanResponse_CODE_NO_PERMISSION,
				ErrMsg: "没有权限",
				Plan:   emptyInfo,
			}, nil
		}
		exist, err := roadmap_dao.RoadmapMemberDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		if !exist {
			return &roadmap_plan_api.GetPlanResponse{
				Code:   roadmap_plan_api.GetPlanResponse_CODE_NO_PERMISSION,
				ErrMsg: "没有权限",
				Plan:   emptyInfo,
			}, nil
		}
	}

	planItem, err := roadmap_dao.PlanInfoDao.Get(ctx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
	if err != nil {
		return &roadmap_plan_api.GetPlanResponse{
			Code:   roadmap_plan_api.GetPlanResponse_CODE_NO_PLAN,
			ErrMsg: "成长计划不存在",
			Plan:   emptyInfo,
		}, nil
	}

	return &roadmap_plan_api.GetPlanResponse{
		Code: roadmap_plan_api.GetPlanResponse_CODE_OK,
		Plan: planItem.ToPlanInfo(),
	}, nil
}

func (apiImpl *RoadmapPlanApiImpl) RemovePlan(ctx context.Context, req *roadmap_plan_api.RemovePlanRequest) (*roadmap_plan_api.RemovePlanResponse, error) {
	sessItem, ok := dao.CacheDao.GetSession(ctx, req.SessionId)
	if !ok {
		return &roadmap_plan_api.RemovePlanResponse{
			Code:   roadmap_plan_api.RemovePlanResponse_CODE_WRONG_SESSION,
			ErrMsg: "会话不存在",
		}, nil
	}
	roadmapItem, err := roadmap_dao.RoadmapInfoDao.Get(ctx, req.RoadmapId)
	if err != nil {
		return &roadmap_plan_api.RemovePlanResponse{
			Code:   roadmap_plan_api.RemovePlanResponse_CODE_NO_ROADMAP,
			ErrMsg: "成长路线图不存在",
		}, nil
	}
	//只有路线图创建者有权限
	if roadmapItem.OwnerUserId != sessItem.UserId {
		return &roadmap_plan_api.RemovePlanResponse{
			Code:   roadmap_plan_api.RemovePlanResponse_CODE_NO_PERMISSION,
			ErrMsg: "没有权限",
		}, nil
	}
	exist, err := roadmap_dao.PlanInfoDao.Exist(ctx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
	if err != nil {
		return nil, err
	}
	if !exist {
		return &roadmap_plan_api.RemovePlanResponse{
			Code: roadmap_plan_api.RemovePlanResponse_CODE_OK,
		}, nil
	}

	dbSess, err := dao.StartSession()
	if err != nil {
		return nil, err
	}
	defer dao.EndSession(dbSess)

	ret, err := dbSess.WithTransaction(ctx, func(sessCtx context.Context) (interface{}, error) {
		err = roadmap_dao.PlanInfoDao.Remove(sessCtx, req.RoadmapId, req.TargetMemberUserId, req.PlanId)
		if err != nil {
			return nil, err
		}
		count, err := roadmap_dao.PlanInfoDao.Count(sessCtx, req.RoadmapId, req.TargetMemberUserId)
		if err != nil {
			return nil, err
		}
		err = roadmap_dao.RoadmapMemberDao.UpdatePlanCount(sessCtx, req.RoadmapId, req.TargetMemberUserId, count)
		if err != nil {
			return nil, err
		}
		return &roadmap_plan_api.RemovePlanResponse{
			Code: roadmap_plan_api.RemovePlanResponse_CODE_OK,
		}, nil
	})
	if err != nil {
		return nil, err
	}
	return ret.(*roadmap_plan_api.RemovePlanResponse), nil
}
